//
//  JDOrderListViewController.swift
//  DragonWallet
//
//  Created by volantPig on 2024/4/14.
//

import UIKit
import JXSegmentedView

class JDOrderListViewController: JDBaseViewController {

    var currentIndex = 0
    
    var isFromPaySuccess: Bool = false
    
    var topBarView: UIView?
    
    var segmentedTitleDataSource = JXSegmentedTitleDataSource()
    var segmentedView = JXSegmentedView()
    lazy var listContainerView: JXSegmentedListContainerView = {
        return JXSegmentedListContainerView(dataSource: self)
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupConfig()
        self.setupUIElements()
        NotificationCenter.default.addObserver(self, selector: #selector(changeCurrentIndex(_:)), name: NSNotification.Name("changeOrderIndex"), object: nil)
        // Do any additional setup after loading the view.
    }
    
    @objc func changeCurrentIndex(_ notification: Notification) {
            
        if let changeIndex = notification.object as? Int {
            self.currentIndex = changeIndex
            self.segmentedView.selectItemAt(index: self.currentIndex)
        }
    }
    
    
    func setupConfig() {
        self.setupNavigationTitle("订单")
        self.setupLeftBackItem()
    }
    
    override func backAction() {
        if isFromPaySuccess {
            self.navigationController?.popToRootViewController(animated: true)
        } else {
            super.backAction()
        }
    }
    

    func setupUIElements() {
        self.topBarView = UIView()
        //46
        self.view.addSubview(self.topBarView!)
        self.topBarView?.snp.makeConstraints({ make in
            make.left.right.equalTo(self.view)
            make.top.equalTo(self.view).offset(statusBarHeight+44)
            make.height.equalTo(46)
        })
        self.topBarView?.backgroundColor = .red
        
        
        //1、初始化JXSegmentedView
        segmentedView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 46)
        segmentedView.backgroundColor = color("#F5F6F8")
        segmentedView.clipsToBounds = true
        segmentedView.delegate = self
        segmentedView.dataSource = segmentedTitleDataSource
//        segmentedView.contentScrollView = self.contentScrollView
        
        //2、配置数据源
        //segmentedViewDataSource一定要通过属性强持有！！！！！！！！！
        
        segmentedTitleDataSource.titles = ["全部订单","待付款","待发货", "待取货", "已完成","已拒单"]//["待付款", "已接单", "待取货", "已完成", "已拒单"]
//        segmentedTitleDataSource.titles = ["待付款", "已付款", "待取货", "已完成"]//["待付款", "已接单", "待取货", "已完成", "已拒单"]
        segmentedTitleDataSource.isTitleColorGradientEnabled = true
        segmentedTitleDataSource.titleSelectedFont = UIFont.systemFont(ofSize: 15, weight: .semibold)
        segmentedTitleDataSource.titleNormalFont = UIFont.systemFont(ofSize: 15, weight: .regular)
        segmentedTitleDataSource.titleNormalColor = UIColor.init(hexString: "#333333")
        segmentedTitleDataSource.titleSelectedColor = UIColor.init(hexString: "#333333")
        
        //3、配置指示器
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorWidth = 25// JXSegmentedViewAutomaticDimension
        indicator.lineStyle = .lengthen
        segmentedView.indicators = [indicator]

        //4、配置JXSegmentedView的属性
        self.topBarView?.addSubview(segmentedView)

        //5、初始化JXSegmentedListContainerView
        // TODO: 这个地方还需要适配
        listContainerView.frame = CGRectMake(0, 46+statusBarHeight+44, screenWidth, screenHeight-46-statusBarHeight-44+UIDevice().safeAreaInset.bottom)
        self.view.addSubview(listContainerView)
        listContainerView.backgroundColor = .commonColor()

        //6、将listContainerView.scrollView和segmentedView.contentScrollView进行关联
        self.segmentedView.listContainer = listContainerView
        
        self.segmentedView.reloadData()
        
        self.segmentedView.selectItemAt(index: self.currentIndex)
        
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension JDOrderListViewController: JXSegmentedViewDelegate {
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {

    }
    func segmentedView(_ segmentedView: JXSegmentedView, didScrollSelectedItemAt index: Int) {
        
    }
}


extension JDOrderListViewController: JXSegmentedListContainerViewDataSource {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return segmentedTitleDataSource.dataSource.count
    }

    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        if index == 0 {
            // 全部
            let allOrderListVC = JDAllOrderListViewController()
            allOrderListVC.baseVC = self
            return allOrderListVC
        } else if index == 1 {
            // 待付款
            let waittingPayListVC = JDWaittingPayListViewController()
            waittingPayListVC.baseVC = self
            return waittingPayListVC
        }else if index == 2 {
            // 已付款 - 待发货
            let alreadyPayVC = JDAlreadyPayViewController()
            alreadyPayVC.baseVC = self
            return alreadyPayVC
        }else if index == 3 {
            // 待取货
            let pickupVC = JDPickupViewController()
            pickupVC.baseVC = self
            return pickupVC
        }else if index == 4 {
            // 已完成
            let completedVC = JDCompletedViewController()
            completedVC.baseVC = self
            return completedVC
        } else {
            // 已拒单
            let sendingVC = JDRefuseOrderViewController()
            sendingVC.baseVC = self
            return sendingVC
        }
    }
}
